
default_server = 'http://182.61.11.234:3535/'

// 自动获取当前页的链接和标题，添加书签
function api_add_tag_with_plugin(category_id, callback){
    get_current_page(function (title, url){
        api_add_tag(title, url, category_id, callback)
    })
}

// 手动添加书签
function api_add_tag(title, url, category_id, callback){
    var data = {
        name: title,
        url: window.btoa(unescape(encodeURIComponent(url)))
    };
    if(category_id != null && category_id !== ""){
        data['category_id'] = category_id
    }
    common_post(
        '/api/add_tag',
        data,
        callback
    )
}

// 获取所有书签
function api_get_all_tag(callback){
    common_post(
        '/api/get_all_tag',
        {

        },
        callback
    )
}

// 删除书签
function api_del_tag(tag_id, callback){
    common_post(
        '/api/del_tag',
        {tag_id:tag_id},
        callback
    )
}

//新增分类
function api_add_category(category_name, callback){
    common_post(
        '/api/add_category',
        {name: category_name},
        callback
    )
}

//移动书签
function api_move_tag(tag_id, category_id, callback){
    common_post(
        '/api/move_tag',
        {
            tag_id: tag_id,
            category_id: category_id
        },
        callback
    )
}

//删除分类
function api_del_category(category_id, callback){
    common_post(
        '/api/del_category',
        {category_id:category_id},
        callback
    )
}

//获取所有分类
function api_get_all_category(callback){
    common_post(
        '/api/get_all_categories',
        {},
        callback
    )
}

// 登录
function api_login(username, password, callback){
    common_post(
        '/api/login',
        {
            username:username,
            password:password
        },
        callback
    )
}


// 注销
function api_logout(callback){
    common_post(
        '/api/logout',
        {},
        callback
    )
}

// 注册
function api_register(username, password, callback){
    common_post(
        '/api/register',
        {
            username:username,
            password:password
        },
        callback
    )
}

function api_get_user_info(callback){
    common_post(
        '/api/get_info',
        {},
        callback
    )
}

function api_get_current_page(callback){
    chrome.tabs.getSelected(null, function (tab) {
        callback(tab.title, tab.url)
    });
}

function is_empty(text){
    return text == null || text.length === 0;

}

/*
* ----------以下的不用管，为本api.js的自身函数-------------
* */

function common_error(xhr, error, exception){
    if(xhr.status === 401){
        alert('登录过期，请重新登录');
        location.replace('/webpage/pop_up/login.html?jump='+window.location.href);
    }else{
        alert(error)
    }

}

function common_post(path, data, success_callback){
    $.ajax({
        url: get_server_url() + path,
        type: 'POST',
        data: data,
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success: function (data, status, xhr){
            if(xhr.status === 200){
                success_callback(data)
            }
            else if(xhr.status === 401){
                alert('登录过期，请重新登录');
                location.replace('/webpage/pop_up/login.html?jump='+window.location.href);
            }
            else{
                alert(data)
            }
        },
        error: common_error
    })
}

function get_current_page(callback){
    chrome.tabs.getSelected(null, function (tab) {
        callback(tab.title, tab.url)
    });
}

function get_server_url(){
    var url = localStorage.getItem("server");
    if(url == null){
        url = default_server
    }
    return url;
}

function set_server_url(url){
    localStorage.setItem("server", url)
}

function has_set_server(){
    return localStorage.getItem("server") != null;
}
